<template>
  <div class="materialCategory-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="编号" prop="materialTypeNumber">
          <el-input
            placeholder="请输入编号"
            v-model="searchForm.materialTypeNumber"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增物料分类
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
        >
          <el-table-column prop="materialTypeNumber" label="编号" width="180" />

          <el-table-column
            prop="materialTypeName"
            label="类型名称"
            width="240"
          />
          <el-table-column prop="enabled" label="状态" width="190">
            <template #default="scope">
              <el-tag
                :type="scope.row.enabled === 0 ? 'success' : 'danger'"
                disable-transitions
              >
                {{ scope.row.enabled === 0 ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="220" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->
              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.enabled == 0"
                type="danger"
                size="small"
                @click.prevent="showEnabledDialog(scope.row, 2)"
                >禁用</el-button
              >
              <el-button
                v-else
                type="success"
                size="small"
                @click.prevent="showEnabledDialog(scope.row, 2)"
                >启用</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <!-- <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >禁用</el-button
              > -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <MaterialCategoryAddUpdate
      v-model="addUpdateRef"
    ></MaterialCategoryAddUpdate>
    <MaterialCategoryRemove v-model="removeRef"></MaterialCategoryRemove>
    <MaterialCategoryEnable v-model="enabledRef"></MaterialCategoryEnable>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getMaterialCategoryAllList,
  getMaterialCategoryPagination,
} from '@/api/fms-material-category.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import MaterialCategoryRemove from './MaterialCategoryRemove.vue'
import MaterialCategoryAddUpdate from './MaterialCategoryAddUpdate.vue'
import { getCanUseCode } from '@/api/fms-code-dict.api'
import MaterialCategoryEnable from './MaterialCategoryEnable.vue'
// const userPermission = useUserPermission();

const enabledRef = reactive({
  visible: false,
  form: {
    id: '',
    enabled: '',
  },
  refreshDataList: () => {
    getDataList()
  },
})

const showEnabledDialog = (item: any) => {
  enabledRef.visible = true
  enabledRef.form.id = item.id
  enabledRef.form.enabled = item.enabled ? '0' : '1'
}

/****
 * =============== 新增、修改相关操作 =====================
 */
const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `物料分类`,
  curSelectRow: {},
  roleList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

const requestCanUseCode = () => {
  getCanUseCode({ type: 4 })
    .then((res: any) => {
      console.log('getCanUseCode: ', res)
      addUpdateRef.form.materialTypeNumber = res
    })
    .catch((err) => {
      ElMessage.error(err.msg)
    })
}

const showAddEditDialog = (item: any, type: number) => {
  //新增 物料分类
  if (item.target) {
    let form = {
      id: null,
      enabled: '',
      remark: '',
      materialTypeNumber: '',
      materialTypeName: '',
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    requestCanUseCode()
    return
  }

  let form = {
    id: item.id,
    enabled: item.enabled,
    remark: item.remark,
    materialTypeNumber: item.materialTypeNumber,
    materialTypeName: item.materialTypeName,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: '物料分类',
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})
const showRemoveDialog = (item: any) => {
  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  enabled: '',
  createTime: '',
  remark: '',
  materialTypeNumber: '',
  materialTypeName: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let enabled = searchForm.value.enabled
  if (enabled) {
    searchCondition.enabled = enabled
  }
  let createTime = searchForm.value.createTime
  if (createTime) {
    searchCondition.createTime = createTime
  }
  let remark = searchForm.value.remark
  if (remark) {
    searchCondition.remark = remark
  }
  let materialTypeNumber = searchForm.value.materialTypeNumber
  if (materialTypeNumber) {
    searchCondition.materialTypeNumber = materialTypeNumber
  }
  let materialTypeName = searchForm.value.materialTypeName
  if (materialTypeName) {
    searchCondition.materialTypeName = materialTypeName
  }

  tableLoading.value = true
  getMaterialCategoryPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.materialCategory-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.materialCategory-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
